package com.lance.answer.gui.answer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * <p><p>
 * <p>
 * 处理学生的考试json
 *
 * @author : fengx
 * @since JDK8.0
 * create by fengx on 2021/7/15 9:34
 */
public class DealStudentTestJsonString2 {

    public static void main(String[] args) throws Exception {


        //D:\\answer\\一体化\\deal\\
        for (File file : new File("D:\\xinjiang5").listFiles()) {
            System.err.println(file.getName());
            if (!file.isDirectory()) {
                continue;
            }
            // String dest = "D:\\answer\\一体化\\"+"deal\\"+file.getName();
            try {
                fix(file, "d://test//deal//" + file.getName() + ".json");

            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("error::" + file.getName());
                Thread.sleep(200000);
            }

      //      Thread.sleep(2000000);

        }


    }

    public static void fix(File directory, String dest) throws IOException {

        List<Question> questions = Lists.newArrayList();
        Set<String> quesetionSet = Sets.newHashSet();
        // 获取每个题目的最高分
        Map<String, Double> scoreByTitle = Maps.newHashMap();

        for (File file : directory.listFiles()) {
            if (!file.getName().endsWith(".json")) {
                continue;
            }
            StringBuilder sb = new StringBuilder();
            for (String s : IOUtils.readLines(new FileInputStream(file))) {
                sb.append(s);
            }

            String result = sb.toString();
            Map<String, Object> content = JSON.parseObject(result, Map.class);

            Map<String, String> map = JSON.parseObject(String.valueOf(content.get("content")), Map.class);

            // 试卷题目信息
            String paperInfo = map.get("paperInfo");
            System.err.println("paperInfo:" + paperInfo);
            Map<String, Object> paperInfoMap = JSON.parseObject(paperInfo, Map.class);

            String sections = String.valueOf(paperInfoMap.get("Sections"));
            System.err.println("Sections:" + sections);
            List<Section> sectionList = JSON.parseArray(sections, Section.class);
            // 问题类型
            Map<String, String> questionTypeMap = Maps.newHashMap();
            Map<String, Integer> questionTypeMapId = Maps.newHashMap();
            for (Section section : sectionList) {
                for (String itemId : section.getItemID()) {
                    questionTypeMap.put(itemId, section.getTitle());
                    questionTypeMapId.put(itemId, section.getType());
                }
            }

            //  题目答案信息
            System.err.println("map:" + JSON.toJSONString(map));
            String answerInfo = map.get("answerInfo");
            System.err.println("answerInfoString:" + answerInfo);
            System.err.println("answerInfo:" + answerInfo);
            List<AnswerInfo> answerInfoList = JSON.parseArray(answerInfo, AnswerInfo.class);

            // 在这里面判断得分是多少，judgeInfo
            String judgeInfoString = map.get("judgeInfo");
            List<JudgeInfo> judgeInfoList = JSON.parseArray(judgeInfoString, JudgeInfo.class);
            Map<String, JudgeInfo> judgeInfoMapByItemId = Maps.newHashMap();
            judgeInfoList.forEach(e -> judgeInfoMapByItemId.put(e.getI1(), e));


            System.err.println("answerInfoList:" + JSON.toJSONString(answerInfoList));
            Map<String, AnswerInfo> answerInfoMapByItemId = Maps.newHashMap();
            answerInfoList.forEach(e -> answerInfoMapByItemId.put(e.getI1(), e));


            String items = String.valueOf(paperInfoMap.get("Items"));
            System.err.println("items:" + items);
            List<Item> itemList = JSON.parseArray(items, Item.class);


            for (Item item : itemList) {
                // I1 itemId
                String itemId = item.getI1();
                // I2 题目
                String title = item.getI2();

                JudgeInfo judgeInfo = judgeInfoMapByItemId.get(itemId);
                if (null == judgeInfo || judgeInfo.getI16() == 0.0) {
                    continue;
                }
                if (scoreByTitle.get(title) != null && judgeInfo.getI16() <= scoreByTitle.get(title)) {
                    continue;
                }
                scoreByTitle.put(title, judgeInfo.getI16());


                // I6 答案集合 不是正确答案
                // FIXME 题目类型在Sections里面
                List<String> answers = item.getI6();
                Question question = new Question();
                questions.add(question);
                question.setQuestionCode(itemId);
                question.setQuestionTypeDesc(questionTypeMap.get(itemId));
                question.setQuestionType(questionTypeMapId.get(itemId));
                question.setTitle(title);
                question.setScore(judgeInfo.getI16());
                List<com.lance.answer.dto.Item> questionItems = Lists.newArrayList();

                for (String answer : answers) {
                    //  fixme 0是对  1是错  这个要处理一下  而且有的答案里面有标签 需要用正则表达式去掉
                    com.lance.answer.dto.Item questionItem = new com.lance.answer.dto.Item(answer, "");
                    questionItems.add(questionItem);
                }
                System.err.println("answers" + JSON.toJSONString(answers));

                List<String> correctList = Lists.newArrayList();
                // I15可能为空 也不一定是正确答案，只是用户自己勾选的。
                List<String> i15 = answerInfoMapByItemId.get(itemId).getI15();
                System.err.println("i15:i15" + JSON.toJSONString(i15));

                try {
                    // i15里面可能为-1和1，-1的情况说明没有填答案
                    for (String s : i15) {
                        int index = Integer.parseInt(s);
                        correctList.add(answers.get(index));
//                    }
                    }
                } catch (Exception e) {

                }
                System.err.println("correctList:" + correctList);
                String[] correctArray = new String[]{};
                correctArray = correctList.toArray(correctArray);
                System.err.println("correctArray:" + JSON.toJSONString(correctArray));
                question.setCorrectArray(correctArray);
                question.setItems(questionItems);

            }
        }

        List<Map<String, Object>> answers = new ArrayList<>();
        for (Question question : questions) {
            String title = question.getTitle();
            title = Util.dealContent(title);
            Map<String, Object> answerMap = new HashMap<>();
            answerMap.put("questionType", question.getQuestionType());
//            if(question.getQuestionType()==6||question.getQuestionType()==4){
//                answerMap.put("answerType","send");
//            }else {
//                answerMap.put("answerType","click");
//            }

            answerMap.put("answerType", "click");
            answerMap.put("questionTypeDesc", question.getQuestionTypeDesc());
            answerMap.put("questionTitle", title);
            //   answerMap.put("questionCode", MD5Utils.encode(subjectName + title));
            answerMap.put("questionCode", question.getQuestionCode());
            answerMap.put("items", question.getItems());
            answerMap.put("answers", question.getCorrectArray());
            answerMap.put("score", question.getScore());
            answers.add(answerMap);
        }

        FileUtils.write(new File(dest), JSONObject.toJSONString(answers, SerializerFeature.PrettyFormat), "utf-8");


    }
}
